#pragma once

#include<iostream>
#include<algorithm>

namespace bit
{

	template<class T>
	struct list_node
	{
		list_node<T>* _next;
		list_node<T>* _prev;
		T _data;

		list_node(const T& x = T())
			:_next(nullptr),_prev(nullptr),_data(x)
		{ }
	};

	template<class T, class Ref,class Ptr>
	struct list_iterator
	{
		using Self = list_iterator<T, Ref,Ptr>;
		using Node = list_node<T>;
		Node* _it;

		list_iterator(Node* x)
			:_it(x)
		{
		}
		Self& operator++()
		{
			_it = _it->_next;
			return *this;
		}
		Self& operator++(int)
		{
			Self tmp = *this;
			_it = _it->_next;
			return tmp;
		}
		Self& operator--()
		{
			_it = _it->_prev;
			return *this;
		}
		Self& operator--(int)
		{
			Self tmp = *this;
			_it = _it->_prev;
			return tmp;
		}
		Ref operator*()
		{
			return _it->_data;
		}
		bool operator==(const Self& it)
		{
			return _it == it._it;
		}
		bool operator!=(const Self& it)
		{
			return _it != it._it;
		}
		Ptr operator->()
		{
			return &_it->_data;
		}
	};

	template<class T>
	class list
	{	
	public:
		using Node = list_node<T>;
		using iterator = list_iterator<T, T&, T*>;
		using const_iterator = list_iterator<T, const T&, const T*>;
		list()
		{
			empty_init();
		}
		~list()
		{
			clear();
			delete _head;
			_head = nullptr;
			_size = 0;
		}
		list(initializer_list<T> li)
		{
			empty_init();
			for (auto& e : li)
			{
				push_back(e);
			}
		}
		template<class InputIterator>
		list(InputIterator begin, InputIterator end)
		{
			empty_init();
			while (begin != end)
			{
				push_back(*begin);
				++begin;
			}
		}
		list(size_t n, T val = T())
		{
			empty_init();
			for (int i = 0; i < n; ++i)
			{
				push_back(val);
			}
		}
		list(int n, T val = T())
		{
			empty_init();
			for (int i = 0; i < n; ++i)
			{
				push_back(val);
			}

		}
		list(const list<T>& li)
		{
			list<T> tmp(li.begin(), li.end());
			swap(tmp);
		}
		list<T>& operator=(const list<T> tmp)
		{
			swap(tmp);
			return *this;
		}
		void swap(const list<T> li)
		{
			swap(_head, li._head);
			swap(_size, li._size);
		}
		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}
		void push_back(const T& x)
		{
			insert(end(), x);
		}
		void push_front(const T& x)
		{
			insert(begin(), x);
		}
		void pop_back()
		{
			erase(end());
		}
		void pop_front()
		{
			erase(begin());
		}
		iterator erase(iterator pos)
		{
			Node* next = pos._it->_next;
			Node* prev = pos._it->_prev;
			prev->_next = next;
			next->_prev = prev;
			delete pos._it;
			_size--;
			return iterator(next);
		}
		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erase(it);
			}
		}
		void insert(iterator pos, const T& x)
		{
			Node* cur = pos._it;
			Node* newnode = new Node(x);
			newnode->_next = cur;
			newnode->_prev = cur->_prev;
			cur->_prev->_next = newnode;
			cur->_prev = newnode;
			_size++;
		}
		size_t size()const
		{
			return _size;
		}
		iterator begin()
		{
			return iterator(_head->_next);
		}
		iterator end()
		{
			return iterator(_head);
		}

	private:
		Node* _head;
		size_t _size = 0;
	};

}